<?php
	namespace Home\Model;
	use Think\Model;

	class UserModel extends CommonModel{


		protected $_validate = array(
			array('username','/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/','账号名称格式错误',1 , 'regex'), // 在新增的时候验证name字段是否唯一
			array('password','require','密码格式不正确',0 ), // 自定义函数验证密码格式

		);


		// 登录
		public function userlogin(){


			$code = I('post.verify');
			$post = I('post.');
			$username = I('post.username');
			$password = I('post.password');

			//验证验证码
			$verify = new \Think\Verify();
			$check = $verify->check($code);

			// 验证验证码
			if ($check) {
				// 数据验证
				if ($this->create($post)) {
					// 验证账号
					$map['username'] = ['eq' , $username];
					$res = $this->where($map)->select();
					if ($res) {
						// 验证状态
						if ($res[0]['status'] == 1) {
							// 验证密码
							if ($res[0]['password'] == md5($password)) {

								session('homeuser',$res[0]['username'] );
								session('user_id',$res[0]['user_id'] );

							}else{
								echo '密码不正确';
							}

						}else{
							echo '状态为禁用';
						}
					}else{
						echo '账号名称不存在';
					}
				}else{
					echo '账号名称格式或密码错误';
				}
			}else{
				echo '验证码错误';
			}

		}

		// 验证码
		public function code(){

			// 验证码
			$config =    array(
				'fontSize'    =>    30,    // 验证码字体大小
				'length'      =>    4,     // 验证码位数
				'useNoise'    =>    false, // 关闭验证码杂点
				'useCurve'    =>    false,
				// 'fontttf'     =>    5.ttf,
			);

			$Verify =     new \Think\Verify($config);
			$code = $Verify->entry();

			return array('code' => $code);


		}

		public function registeruser(){

			$rules = array(
				//新增的时候验证name字段是否唯一
				array('username','','帐号名称已经存在！',1,'unique',1),
				// 验证确认密码是否和密码一致
		        array('repassword','password','确认密码不正确',1,'confirm'),
		    );


			// 名称正则验证
			$username = I('post.username');
			$preg = '/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/';
			if(!preg_match($preg ,$username)){
				echo "请输4-15位用户名*(不能以数字开头)";
				exit;
			};

			// 密码正则验证
			$password = I('post.password');
			$preg = '/^[a-zA-Z0-9_]{4,15}$/';
			if(!preg_match($preg ,$username)){
				echo "请输4-15位密码";
				exit;
			};

			// 确认密码正则验证
			$repassword = I('post.repassword');
			$preg = '/^[a-zA-Z0-9_]{4,15}$/';
			if(!preg_match($preg ,$repassword)){
				echo "请输4-15位确认密码";
				exit;
			};

			$post = I('post');

			$registercode = I('post.verify');
			//验证验证码
			$verify = new \Think\Verify();
			$registercheck = $verify->check($registercode);

			// 验证验证码
			if ($registercheck) {
				// 验证数据 名称是否唯一 密码是否重复
				$res = $this->validate($rules)->create($post);
				if ($res) {
					$res['status'] = 1;
					$res['password'] = md5($res['password']);
					$result = $this->add($res);
					if ($result) {
						$integralion = M('integralion');
						$list = [];
						$list['user_id'] = $result;
						$list['type'] = '注册';
						$list['integral'] = 200;
						$list['ctime'] = time();
						$integralion->add($list);

					}else{
						echo '网络错误,注册失败';
					}
				}else{
					echo '密码不一致';
				}
			}else{
				echo '验证码错误';
			}
		}



		// 快速注册验证
		public function registe(){
			$rules = array(
				//新增的时候验证name字段是否唯一
				array('mobile','','此号码已注册,请直接登录！',1,'unique',1),
		    );

			// 手机号码正则验证
			$mobile = I('post.mobile');
			$preg = '/^1[35689]\d{9}$/';
			if(!preg_match($preg ,$mobile)){
				echo "请输入正确的电话号码";
				exit;
			};

			$post = I('post.');
			//提交时验证post与session是否一致

			if($post['mobile']!=$_SESSION['mobile'] or $post['mobile_code']!=$_SESSION['mobile_code'] or empty($post['mobile']) or empty($post['mobile_code'])){
				echo   '手机验证码输入错误。';
			}else{
				$_SESSION['mobile'] = '';
				$_SESSION['mobile_code'] = '';

				$detail = M('user_detail');
				$map['username'] = ['eq' , $post['mobile']];
				$result = $this->where($map)->find();
				$map['tel'] = ['eq' , $post['mobile']];
				$redetail = $detail->where($map)->find();

				if($result or $redetail){
					echo '电话号码已存在';
				}else{
					$res['status'] = 1;
					$res['username'] = "wq".$post['mobile'];
                    $password = substr($post['mobile'], -6, 6);
					$res['password'] = md5($password);
					$result = $this->add($res);

					if($result){
						session('homeuser',$res['username'] );
						session('user_id',$result );
						$integralion = M('integralion');
						$list = [];
						$list['user_id'] = $result;
						$list['type'] = '注册';
						$list['integral'] = 200;
						$list['ctime'] = time();
						$info = $integralion->add($list);

					}else{
						echo '操作失败!';
					}
				}
			}
		}
	}
